<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属公司">
            <el-select v-model="queryParams.type" placeholder="请选择" style="width: 120px" clearable>
              <el-option
                v-for="dict in dict.type.technician_type1"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
      </el-form-item>
      <el-form-item label="状态">
            <el-select v-model="queryParams.status" placeholder="请选择" style="width: 100px" clearable>
              <el-option
                v-for="dict in dict.type.business_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
      </el-form-item>
      <el-form-item>
           <el-form-item label="技师级别">
            <el-select v-model="queryParams.technicianType" placeholder="请选择" style="width: 100px" clearable>
              <el-option
                v-for="dict in dict.type.technician_type2"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
      </el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

     <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plainicon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
          @click="handleDelete">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-upload2" size="mini" @click="handleOnline">上架</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-download" size="mini" @click="handleOffline">下架</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
 
    <el-table v-loading="loading" :data="technicianList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="20" align="center" />
      <el-table-column label="编号" align="center" prop="id" width="50" />
      <el-table-column label="姓名" align="center" prop="name" width="120"/>
      <el-table-column label="电话" align="center" prop="phone" width="120"/>
      <el-table-column label="所属公司" align="center" prop="source" width="120"/>
      <el-table-column label="技师级别" align="center" prop="technicianType" >
        <template slot-scope="scope">
            <dict-tag :options="dict.type.technician_type2" :value="scope.row.technicianType"/>
        </template>
      </el-table-column>
      <el-table-column label="留言内容" align="center" prop="message" :show-overflow-tooltip="true"/>

      <el-table-column label="状态" align="center" prop="status" width="70">
        <template slot-scope="scope">
            <dict-tag :options="dict.type.business_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button> 
          <el-button v-if="scope.row.status !== '1'" size="mini" type="text" icon="el-icon-upload2" @click="handleOnline(scope.row)">上架</el-button>
          <el-button v-if="scope.row.status === '1'" size="mini" type="text" icon="el-icon-download" @click="handleOffline(scope.row)">下架</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="所属公司" prop="postSort">
          <el-input v-model="form.source" placeholder="请输入所属公司" />
        </el-form-item>
        <el-form-item label="技师级别" prop="technicianType">
            <el-radio-group v-model="form.technicianType">
              <el-radio label="0">普通技师</el-radio>
              <el-radio label="1">优质技师</el-radio>
              <el-radio label="2">今日推荐</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="留言内容" prop="message">
          <el-input v-model="form.message" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import{
  listTechnician,delTechnician,batchUpdateStatus,updateStatus,
  getTechnician,addTechnician,updateTechnician
}
from "@/api/company/technicians"; 

export default {
  name: "Technician",
  dicts: ['business_status', 'technician_type1','technician_type2'],

  data() {
    return {
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 默认展开基础信息
      activeName: '1',
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员表格数据
      technicianList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, name: "姓名不能为空", trigger: "blur" }
        ],
        phone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        message: [
          { required: true, message: "留言内容不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listTechnician(this.queryParams).then(response => {
        this.technicianList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTechnician(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人员";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateTechnician(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTechnician(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + id + '"的数据项？').then(function() {
        return delTechnician(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    // 上架
    handleOnline(row) {
      if (row.id !== undefined) {
        updateStatus({'id':row.id, 'status':'1'}).then(() => {
          this.$modal.msgSuccess('上架成功');
          this.getList();
        }).catch(() => {});
      } else if (this.ids !== undefined && this.ids.length > 0) {
        batchUpdateStatus({'ids':this.ids, 'status':'1'}).then(response => {
          this.$modal.msgSuccess(response.msg);
          this.getList();
        }).catch(() => {});
      } else {
        this.$modal.msgError("请选中要上架的人员");
      }
    },

    // 下架
    handleOffline(row) {
      if (row.id !== undefined) {
        // 单个
        updateStatus({'id':row.id, 'status':'2'}).then(() => {
          this.$modal.msgSuccess('下架成功');
          this.getList();
        }).catch(() => {});
      } else if (this.ids !== undefined && this.ids.length > 0) {
        batchUpdateStatus({'ids':this.ids, 'status':'2'}).then(response => {
          this.$modal.msgSuccess(response.msg);
          this.getList();
        }).catch(() => {});
      } else {
        this.$modal.msgError("请选中要下架的人员");
      }
    },


  }
};
</script>
